//package com.charley.icloud.common.response.utils;
//
//
//import com.alibaba.fastjson.JSON;
//import com.charley.icloud.common.response.code.ResponseMsgConstants;
//import com.charley.icloud.common.exception.ApiFailedException;
//import org.springframework.http.MediaType;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletResponse;
//import java.nio.charset.StandardCharsets;
//
//public class ResponseUtils {
//    public static void success() throws Exception {
//        ResponseMsg responseMsg= new ResponseMsg(ResponseMsgConstants.SUCCESS, ResponseMsgConstants.SUCCESS_MSG,"");
//        String resJson = JSON.toJSONString(responseMsg);
//
//        response(resJson);
//    }
//    public static void success(Object data) throws Exception {
//        ResponseMsg responseMsg = new ResponseMsg(ResponseMsgConstants.SUCCESS, ResponseMsgConstants.SUCCESS_MSG, data != null ? data : "");
//        String resJson = JSON.toJSONString(responseMsg);
//
//        response(resJson);
//    }
//
//    public static void fail() throws Exception {
//        ResponseMsg responseMsg = ResponseMsg.build(ResponseMsgConstants.FAIL, ResponseMsgConstants.FAIL_MSG);
//        String resJson = JSON.toJSONString(responseMsg);
//
//        response(resJson);
//    }
//    public static void fail(String errmsg) throws Exception {
//        ResponseMsg responseMsg = ResponseMsg.build(ResponseMsgConstants.FAIL, errmsg);
//        String resJson = JSON.toJSONString(responseMsg);
//
//        response(resJson);
//    }
//
//    public static void fail(String errmsg, String errcode) throws Exception {
//        ResponseMsg responseMsg = ResponseMsg.build(errcode, errmsg);
//        String resJson = JSON.toJSONString(responseMsg);
//
//        response(resJson);
//    }
//
//
//    private static void response(String content) throws Exception {
//        HttpServletResponse response = getResponse();
//        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
//
//        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
//        response.setContentLength(bytes.length);
//        response.getOutputStream().write(bytes);
//        response.flushBuffer();
//
//        // 为了终止后续代码的换行
//        throw new ApiFailedException();
//    }
//
//    private static HttpServletResponse getResponse() throws Exception {
//        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//
//        HttpServletResponse response = servletRequestAttributes.getResponse();
//
//        if(response == null) {
//            throw new Exception("获取 HttpServletResponse 对象错误");
//        }
//
//        return response;
//    }
//}
//
